package com.jzy.gulimall.order.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.jzy.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author jzy
 * @date 2020/9/23 8:44
 */
@Component
public class AliPayUtil {

    @Resource
    private AlipayClient alipayClient;

    @Value("${alipay.notify_url}")
    private String notifyUrl;

    @Value("${alipay.return_url}")
    private String returnUrl;

    /**
     * aiPay支付
     */
    public String aliPay(PayVo payVo) {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        //timeout_express不应该写死,应该根据订单的创建时间time_expire
        request.setBizContent("{\"out_trade_no\":\"" + payVo.getOutTradeNo() + "\","
                + "\"total_amount\":\"" + payVo.getTotalAmount() + "\","
                + "\"subject\":\""+payVo.getSubject()+"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        try {
            //3、请求支付宝进行付款，并获取支付结果
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "";
    }
}
